<?php

namespace App\Validation;

use Phalcon\Validation;
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\Numericality;
use Phalcon\Validation\Validator\InclusionIn;

class NewsOrgValidation extends BaseValidation
{
    public function initialize()
    {
		$this->add(
			"id",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"reco_ids",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  "allowEmpty" => true
				  ]
			)
		);

		$this->add(
			"city_id",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
					  "allowEmpty" => true
				  ]
			)
		);

		$this->add(
			"creator_id",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
					  "allowEmpty" => true
				  ]
			)
		);

		$this->add(
			"title",
			new PresenceOf(
				  [
					  "message" => ":field is required",
				  ]
			)
		);

		$this->add(
			"sub_title",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"tags",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"summary",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  "allowEmpty" => true,
				  ]
			)
		);


		$this->add(
			"type_id",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"type_id",
			new InclusionIn(
				  [
					  "field" => "type_id",
					  "domain" => [1,2,3,4,5,6],//展示类型 1文字,2图左字右,3多图,4视频,5相册,6广告
					  "message" => ":field is not included in 1 or 2 or 3 or 4 or 5 or 6",
				  ]
			)
		);

		$this->add(
			"news_type",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"thumb",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  "allowEmpty" => true,
				  ]
			)
		);



		$this->add(
			"content",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"sources",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  "allowEmpty" => true,
				  ]
			)
		);
        $this->add(
            "topic_id",
            new PresenceOf(
                [
                    "message" => ":field is required",
                    "allowEmpty" => true,
                ]
            )
        );
        $this->add(
            "reco_ids",
            new PresenceOf(
                [
                    "message" => ":field is required",
                    "allowEmpty" => true,
                ]
            )
        );

        $this->add(
            "news_type",
            new PresenceOf(
                [
                    "message" => ":field is required",
                ]
            )
        );

        $this->add(
            "topics",
            new PresenceOf(
                [
                    "message" => ":field is required",
                    "allowEmpty" => true,
                ]
            )
        );

        $this->add(
            "topic_sign",
            new PresenceOf(
                [
                    "message" => ":field is required",
                    "allowEmpty" => true,
                ]
            )
        );

        $this->add(
            "publish_time",
            new PresenceOf(
                [
                    "message" => ":field is required",
                    "allowEmpty" => true,
                ]
            )
        );

        $this->add(
            "is_display",
            new Numericality(
				  [
					  "message" => ":field is not numeric",
					  "allowEmpty" => true,
				  ]
			)
        );

        //2017-10-25 +
		$this->add(
			"sub_type",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
					  "allowEmpty" => true,
				  ]
			)
		);

		//2017-11-09 +
		$this->add(
			"visit",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
				  ]
			)
		);
    }
	
	//验证之前
	public function beforeValidation($data, $entity, $messages)
    {
    	$data->is_self = property_exists($data,'is_self') ? $data->is_self : 0;

        $data->sources = property_exists($data,'sources') ? $data->sources : '觅窝';
		$data->visit = property_exists($data,'visit') ? $data->visit : 0;
		$data->comment = property_exists($data,'comment') ? $data->comment : 0;
		$data->is_display = property_exists($data,'is_display') ? $data->is_display : 1;
		$data->is_del = property_exists($data,'is_del') ? $data->is_del : 0;
		$data->order_by = property_exists($data,'order_by') ? $data->order_by : 100;
		$data->is_stick = property_exists($data,'is_stick') ? $data->is_stick : 0;
		$data->news_type = property_exists($data,'news_type') ? $data->news_type : 'putong';
		$data->publish_time = property_exists($data,'publish_time') ? $data->publish_time : '';

		//2017-10-25 +
		$data->sub_type = property_exists($data,'sub_type') ? $data->sub_type : 1;

		//2017-11-28 +
		$data->creator_id = property_exists($data,'creator_id') ? $data->creator_id : 0;

		$data->update_time = property_exists($data,'update_time') ? $data->update_time : date('Y-m-d H:i:s', time());
		$data->create_time = property_exists($data,'create_time') ? $data->create_time : date('Y-m-d H:i:s', time());
        return true;
    }
}